package com.blank.shop.security.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <br/>Date 2021/9/9
 * <br/>Time 22:55:48
 *
 * @author _blank
 */
@RestController
public class PersonController {

    /**
     * 多个请求间共享认证结果
     */
    @GetMapping(path = {"me1"})
    public ResponseEntity<Authentication> me1() {
        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return ResponseEntity.ok(authentication);
    }

    @GetMapping(path = {"me2"})
    public ResponseEntity<Authentication> me2(Authentication authentication) {
        return ResponseEntity.ok(authentication);
    }

    @GetMapping(path = {"me3"})
    public ResponseEntity<UserDetails> me3(@AuthenticationPrincipal UserDetails userDetails) {
        return ResponseEntity.ok(userDetails);
    }

    @GetMapping(path = {"hello"})
    public ResponseEntity<String> hello() {
        return ResponseEntity.ok("hello spring security");
    }

}
